package com.xx.doublexchat.common.common.thread;
import lombok.AllArgsConstructor;

import java.util.concurrent.ThreadFactory;

@AllArgsConstructor
public class MyThreadFactory implements ThreadFactory {
    /**
     * 单例模式----饿汉式
     */
    private static final MyUncaughtExceptionHandler MY_UNCAUGHT_EXCEPTION_HANDLER = new MyUncaughtExceptionHandler();

    private ThreadFactory original;

    @Override
    public Thread newThread(Runnable r) {
        //执行spring线程自己的创建模式
        Thread thread = original.newThread(r);
        //额外执行我们需要的创建逻辑---添加自定义的异常捕获器
        thread.setUncaughtExceptionHandler(MY_UNCAUGHT_EXCEPTION_HANDLER);
        return thread;
    }
}
